package com.deshan.websys.code.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deshan.websys.code.logger.Loggers;
import com.deshan.websys.code.mapper.CustomerMapper;
import com.deshan.websys.code.mapper.PmsVisitLogMapper;
import com.deshan.websys.code.mapper.VisitorMapper;
import com.deshan.websys.code.model.Customers;
import com.deshan.websys.code.model.PmsVisitLog;
import com.deshan.websys.code.model.vo.VisitLogVO;
import com.deshan.websys.code.service.PmsVisitLogService;
import com.deshan.websys.code.service.UmsUserService;
import com.deshan.websys.code.service.VisitorsService;
import com.deshan.websys.code.utils.Constant;
import com.deshan.websys.code.utils.UuidUtils;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class PmsVisitLogServiceImpl extends ServiceImpl<PmsVisitLogMapper, PmsVisitLog> implements PmsVisitLogService {
	@Resource
	private PmsVisitLogMapper pmsVisitLogMapper;

	@Resource
	private UmsUserService userService;

	@Resource
	private VisitorsService visitorsService;

	@Resource
	private VisitorMapper visitorMapper;

	@Resource
	private CustomerMapper customerMapper;

	@Override
	public int setRecord(String ip) {
		int result = 0;
		PmsVisitLog pmsVisitLog = new PmsVisitLog();
		pmsVisitLog.setId(UuidUtils.shortUuid());
		Date time = Calendar.getInstance().getTime();
//		pmsVisitLog.setCreateTime(LocalDateTime.now());
		result = pmsVisitLogMapper.insert(pmsVisitLog);
		return result;
	}

	@Override
	public int getVisitCount() {
		int result = 0;
		try {
			result = pmsVisitLogMapper.selectCount(null);
		} catch (Exception ex) {
			Loggers.serviceLogger.error("find visitlog error");
		}
		return result;
	}

	@Override
	@Transactional
	public int saveOrUpdate(VisitLogVO visitLogVO) {
		int result = 0;
		PmsVisitLog visitLog;

		Customers customers = customerMapper.selectById(visitLogVO.getCustomerId());

		if (Objects.nonNull(customers)) {
			LambdaQueryWrapper<PmsVisitLog> wrapper = Wrappers.lambdaQuery(PmsVisitLog.class)
					.eq(PmsVisitLog::getLegalCode, visitLogVO.getLegalCode())
					.eq(PmsVisitLog::getColumnCode, visitLogVO.getColumnCode())
					.eq(PmsVisitLog::getCid, visitLogVO.getCustomerId());
			visitLog = getOne(wrapper);
			if (Objects.nonNull(visitLog)) {
				visitLog.setClickCount(visitLog.getClickCount() + 1);
				log.info("法人{}的{}栏目，被{}点击了{}次", visitLog.getLegalCode(), visitLog.getColumnCode(), customers.getId(),
						visitLog.getClickCount());
				result = pmsVisitLogMapper.updateById(visitLog);
				return result;
			}
		}

		customers = new Customers();
		customers.setMainCor(visitLogVO.getLegalCode());
		customers.setId(visitLogVO.getCustomerId());
		customers.setRoleType(Constant.CUSTOMER_ROLE_TYPE);
		customers.setNickname(Constant.VISIT_USER_NICKNAME_PREFIXX + RandomUtil.randomInt(1, Integer.MAX_VALUE));
		customerMapper.insert(customers);

		log.info("法人{}的{}栏目，首次被点击", visitLogVO.getLegalCode(), visitLogVO.getColumnCode());
		visitLog = new PmsVisitLog();
		visitLog.setClickCount(1);
		BeanUtils.copyProperties(visitLogVO, visitLog);
		visitLog.setCid(customers.getId());
		result = pmsVisitLogMapper.insert(visitLog);

		return result;
	}

}
